class Solution
{
public:
    int expressiveWords(string s, vector<string> &words)
    {
        if (s.size() == 0)
        {
            int zeroWordCount = 0;
            for (auto &word : words)
            {
                if (word.size() == 0)
                {
                    ++zeroWordCount;
                }
            }
            return zeroWordCount;
        }
        string compressedS;
        vector<int> repeatCountS;
        setCompressAndRepeat(s, compressedS, repeatCountS);
        int canExpandCount = 0;
        int compressedLen = compressedS.size();
        for (auto &word : words)
        {
            if (word.size() == 0)
            {
                continue;
            }
            string compressedWord;
            vector<int> wordRepeat;
            setCompressAndRepeat(word, compressedWord, wordRepeat);
            if (compressedS != compressedWord)
            {
                continue;
            }
            else
            {
                bool canExpand = true;
                for (int i = 0; i < compressedLen; ++i)
                {
                    if ((repeatCountS[i] < wordRepeat[i]) || (repeatCountS[i] < 3 && repeatCountS[i] > wordRepeat[i]))
                    {
                        canExpand = false;
                        break;
                    }
                }
                if (canExpand)
                {
                    ++canExpandCount;
                }
            }
        }
        return canExpandCount;
    }

    void setCompressAndRepeat(string &origin, string &compressed, vector<int> &repeat)
    {
        char last = origin[0];
        int count = 0;
        for (char c : origin)
        {
            if (c == last)
            {
                ++count;
            }
            else
            {
                repeat.push_back(count);
                compressed += c;
                count = 1;
                last = c;
            }
        }
        repeat.push_back(count);
        compressed += last;
    }
};